package com.ibeeking.found.org.cp.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.org.cp.rest.service.IOrgJobService;
import com.ibeeking.found.org.service.common.dos.OrgJobDO;
import com.ibeeking.found.org.service.common.param.OrgJobParam;
import com.ibeeking.found.org.service.common.query.OrgJobQuery;
import com.ibeeking.found.org.service.common.vo.OrgJobVO;
import com.ibeeking.found.org.service.mapper.OrgJobMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName OrgJobServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-29 14:48
 **/
@Service
public class OrgJobServiceImpl extends ServiceImpl<OrgJobMapper, OrgJobDO> implements IOrgJobService {

    @Resource
    private OrgJobMapper OrgJobMapper;

    @Override
    public List<OrgJobVO> list(OrgJobQuery query) {
        List<OrgJobDO> OrgJobDOS = OrgJobMapper.selectList(new LambdaQueryWrapper<OrgJobDO>()
                .eq(query.getId()!=null ,OrgJobDO::getPublishStatus, query.getId())
                .eq(query.getLevel()!=null ,OrgJobDO::getPublishStatus, query.getLevel())
                .eq(StringUtils.isNotEmpty(query.getCode()),OrgJobDO::getCode, query.getCode())
                .like(StringUtils.isNotEmpty(query.getName()),OrgJobDO::getName, query.getName())
                .eq(query.getPublishStatus()!=null ,OrgJobDO::getPublishStatus,query.getPublishStatus())
                .orderByAsc(OrgJobDO::getSort));
        return JsonUtils.jsonToList(OrgJobDOS, OrgJobVO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(OrgJobParam param) {
        this.checkUnique(param);
        OrgJobDO OrgJobDO = BeanUtil.convertBean(param, OrgJobDO.class);
        OrgJobDO.setId(IdGenerator.getId());
        return OrgJobMapper.insert(OrgJobDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(OrgJobParam param) {
        this.checkUnique(param);
        OrgJobDO OrgJobDO = BeanUtil.convertBean(param, OrgJobDO.class);
        return OrgJobMapper.updateById(OrgJobDO) > 0;
    }

    @Override
    public OrgJobVO view(Long id) {
        OrgJobDO OrgJobDO = OrgJobMapper.selectById(id);
        return BeanUtil.convertBeanByJson(OrgJobDO, OrgJobVO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        OrgJobDO OrgJobDO = OrgJobMapper.selectById(id);
        OrgJobDO.setPublishStatus(status);
        return OrgJobMapper.updateById(OrgJobDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        OrgJobDO OrgJobDO = OrgJobMapper.selectById(id);
        if (OrgJobDO == null) {
            return true;
        }
        PublishStatusEnum.statusCheckException(OrgJobDO.getPublishStatus());
        //删除会员偏好关系表数据
        return OrgJobMapper.deleteById(id) > 0;
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(OrgJobParam param) {
        if (param == null) {
            throw new BusinessException("当前职位数据不存在!");
        }
        //code name确保等级唯一性
        LambdaQueryWrapper<OrgJobDO> queryWrapper = new LambdaQueryWrapper<OrgJobDO>()
                .ne(param.getId() != null, OrgJobDO::getId, param.getId())
                .eq(OrgJobDO::getName, param.getName());
        List<OrgJobDO> dos = OrgJobMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在同名的职位!");
        }
    }

}